/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package unidad.informe;

import DBMS.DataBaseManager;
import clases.Informe;
import java.awt.FileDialog;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DownloadAction;

/**
 *
 * @author carlo
 */
public class DescargarInforme extends DownloadAction{
    
    private static final String SUCCESS = "success";
    private static final String name = "Descargar";

    /**
     * This is the action called from the Struts framework.
     *
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    protected StreamInfo getStreamInfo(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response)
            throws Exception {
        DataBaseManager d = DataBaseManager.getInstance();
        Informe informe = (Informe) form;
        System.out.println("Informe ID" + informe.getIdinforme());
        if (informe.getIdinforme() == null) {
            return null;
        }
        String ruta = d.consultarRutaInforme(informe);
        System.out.println("Pase el informe");
        //Seleccionar path
        String path = guardarArchivo() + "/" + informe.getNombre() + ".pdf"; //guardarArchivo();
        System.out.println("Intentando crear reporte en " + path);
        FileOutputStream f = new FileOutputStream(path);
        
                //****************
try {
   String[] nombre = ruta.split("/");

   // set the http content type to "APPLICATION/OCTET-STREAM
   response.setContentType("APPLICATION/OCTET-STREAM");

   // initialize the http content-disposition header to
   // indicate a file attachment with the default filename
   // "myFile.txt"
   String disHeader = "Attachment;\nFilename=\""+nombre[1]+"\"";
   response.setHeader("Content-Disposition", disHeader);

   // transfer the file byte-by-byte to the response object
   File descargado = new File(ruta);
   FileInputStream fileInputStream = new FileInputStream(descargado);
   int i;
   while ((i=fileInputStream.read())!=-1)
   {
      f.write(i);
   }
   fileInputStream.close();
   f.close();
   }catch(Exception e) // file IO errors
   {
   e.printStackTrace();
}        
        
        
 //********************* 

        //reporte.imprimirArchivoDoc(f);
        String contentType = "application/pdf";
        System.out.println(" ");
        File file = new File(path);
        return new FileStreamInfo(contentType, file);
    }

    public String guardarArchivo() {
        JFileChooser fC = new JFileChooser();
        fC.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        String path = "";
        if (fC.showSaveDialog(fC) == 0) {
            path = fC.getSelectedFile().getAbsolutePath();
        }
        return path;
    }
}
